Skip to content

Fix TopicPartition metadata over-read when metadata isn't NUL-terminated - #2431

Open
Steven Yuen (steveny91) wants to merge 1 commit into
confluentinc:masterfrom
steveny91:fix/topicpartition-metadata-overread
Open

Steven Yuen (steveny91) wants to merge 1 commit into
confluentinc:masterfrom
steveny91:fix/topicpartition-metadata-overread

Conversation

@steveny91

Copy link
Copy Markdown

What

Fixes #2430.

c_part_to_py() passed rktpar->metadata to TopicPartition_new0() without rktpar->metadata_size, and TopicPartition_setup() copied it with strdup(). librdkafka returns metadata as a sized buffer with no guaranteed NUL, so for offsets committed by clients that don't store a terminator (kafka-python, Java, ...) strdup() read past the end of the buffer. Depending on the heap, .metadata came back with trailing garbage, raised UnicodeDecodeError, or the process crashed with SIGSEGV on librdkafka's background thread.

This change threads metadata_size through TopicPartition_new0() into TopicPartition_setup(), which now copies exactly metadata_size bytes and appends a NUL. TopicPartition_init() (Python-constructed partitions) passes strlen(metadata), so its behaviour is unchanged.

  • Buffers from confluent-kafka's own commits already count the NUL in metadata_size, so their copy ends in two NULs and .metadata is unchanged.
  • malloc + memcpy rather than strndup(), because strndup() isn't available on MSVC.
  • c_part_to_py() is the only C-side reader of partition metadata, so this covers every API that returns TopicPartitions (Consumer.committed(), AdminClient.list_consumer_group_offsets(), ...).

Checklist

  • Contains customer facing changes? Including API/behavior changes
    • Bug fix only: TopicPartition.metadata now contains exactly the committed bytes. No API change.
  • Did you add sufficient unit test and/or integration test coverage for this PR?
    • Integration test test_consumer_committed_metadata_without_nul_terminator_returns_committed_bytes in tests/integration/consumer/test_consumer_topicpartition_metadata.py.

References

JIRA: n/a (external contributor)

Test & Review

The new test commits 248 bytes of metadata on 8 partitions with a hand-built OffsetCommit v2 request (confluent-kafka itself always stores the NUL, so it can't produce this case), then reads it back with Consumer.committed() and checks the exact value. 248 bytes makes the failure show on every run under glibc: the allocation's usable size then ends exactly at the next chunk's header, so the over-read picks up non-zero bytes after (nearly) every buffer.

Run against a single apache/kafka:4.2.0 broker (BROKERS=localhost:9092), master built against librdkafka 2.16.0, Ubuntu 24.04 aarch64:

  • Without the fix: fails 10 of 10 runs, at the assertion:

    >       assert [tp.metadata for tp in offsets] == [metadata] * len(partitions)
    E       UnicodeDecodeError: 'utf-8' codec can't decode byte 0x91 in position 248: invalid start byte
    

    Position 248 is the first byte past the committed metadata.

  • With the fix: passes 10 of 10 runs, together with the existing test_consumer_topicpartition_metadata.

  • Unit tests (tests/ without tests/integration and tests/soak): the same tests fail with and without this change in my environment (no AWS credentials, timing-sensitive close-race and memory-safety tests). tests/integration/consumer + tests/integration/admin: the same 3 tests fail on master and with this change (no ACL authorizer, broker config defaults, a timing test), plus one setup error from the trivup SASL cluster.

  • Under jemalloc, with this change, every partition returns exactly the committed bytes: 256 bytes × 2,048 partitions through list_consumer_group_offsets() (3 of 3 runs; the shape that crashed in production) and 248 bytes × 2,048 through committed() (2 of 2).

  • AddressSanitizer, same 256 × 2,048 list_consumer_group_offsets() read: master reports heap-buffer-overflow ... 0 bytes after 256-byte region in strdup ← TopicPartition_setup ← c_part_to_py ← c_parts_to_py ← Admin_background_event_cb; with this change, no report and all partitions correct.

  • flake8 and black --check pass on the test file; clang-format reports nothing in the changed C lines.

The same C change, as a patch on 2.13.2, has also run in production on the workload that originally crashed: the over-read crash stopped (previously within minutes, then none in ~16 hours).

Open questions / Follow-ups

  • The test speaks the Kafka protocol directly to commit unterminated metadata (about 50 lines of struct.pack). Adding kafka-python as a test dependency would be shorter; happy to switch if you'd prefer that.
  • malloc is unchecked, matching the existing strdup() calls in this function. Happy to add a check here.

c_part_to_py() dropped rktpar->metadata_size and TopicPartition_setup()
strdup()'d the metadata, but librdkafka returns it as a sized buffer with
no guaranteed NUL terminator. Offsets committed by clients that store the
exact length (kafka-python, Java) were read past the end of the buffer.

Thread metadata_size through TopicPartition_new0() into
TopicPartition_setup() and copy exactly that many bytes plus a NUL.

Fixes confluentinc#2430.
@confluent-cla-assistant

confluent-cla-assistant Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

🎉 All Contributor License Agreements have been signed. Ready to merge.
✅ steveny91
Please push an empty commit if you would like to re-run the checks to verify CLA status for all contributors.

@steveny91
Steven Yuen (steveny91) marked this pull request as ready for review November 13, 2026 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TopicPartition.metadata over-reads the heap (and can SIGSEGV) when committed metadata isn't NUL-terminated

2 participants